<template>
  <div class="result">
    <!-- 导航栏 -->
    <van-nav-bar
      :title="$route.query.key + '结果'"
      left-arrow
      @click-left="$router.back()"
      fixed
      placeholder
    />
    <!-- 内容 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
        @click="runArticle(item.art_id)"
      />
    </van-list>
  </div>
</template>

<script>
import { searchAPI } from "@/api";
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
    };
  },
  methods: {
    async onLoad() {
      console.log("load调用了", this.loading);
      let res = await searchAPI({
        page: this.page,
        per_page: 10,
        q: this.$route.query.key,
      });
      console.log(res);
      //   console.log(...res.data.data.results);
      this.list.push(...res.data.data.results);
      this.page++;
      this.loading = false;

      //   当数组的长度和文章总数相同表示结束
      if (this.list.length == res.data.data.total_count) {
        this.finished = true;
      }
    },

    runArticle(id) {
      this.$router.push({
        path: "/home/articleNew",
        query: {
          id,
        },
      });
    },
  },
};
</script>

<style>
</style>